package projetstackoverflow

import java.util.Date;

import javax.xml.bind.helpers.Messages;

class MyTagLib {
	
	def ago = { attrs, body ->
		Date now = new Date()
		Date old = attrs.time
		float diff = now.getTime()-old.getTime()
		int diffSecondes = diff / 1000
		def value
		if(diffSecondes < 60)
		{
			value = diffSecondes
			out << body() << message(code:"ago.seconds", args:[value])
		}
		else if(diffSecondes < 3600)
		{
			value = ((int)(diffSecondes / 60))
			out << body() << message(code:"ago.minutes", args:[value])
		}
		else if(diffSecondes < 86400 )
		{
			value = ((int)(diffSecondes / 3600))
			out << body() << message(code:"ago.hours", args:[value])
		}
		else
		{
			value = ((int)(diffSecondes / 86400))
			out << body() << message(code:"ago.days", args:[value])
		}
	 }
	
	/*
	def voteCount= { attrs, body ->
		def message = Message.get(attrs.id)
		int nb = 0
		for(Vote vote : message.votes)
		{
			nb += vote.value
		}
		out << body() << nb
		
	 }*/
	
	def messageInfo = { attrs, body ->
		def mess = Message.get(attrs.id)
		def author = mess.author
		out << body() << '<div class="tag-message-info">'+ago(time:mess.date)+'<br />'
		out << body() << '<a href="'+createLink(controller:'author', action:'show', id:author.id)+'">'+author.pseudo+'</a><br />'
		out << body() << '<span title="'+message(code:"author.reputation.label")+'">'+author.computeReputation()+'</span></div>'
	}
	
	def tagList = { attrs, body ->
		def question = Question.get(attrs.id)
		def html = '<div class="tag-tag-list">'
		for(Tag tag : question.tags.sort{it.value})
		{
			html += '<div class="tag"><a href="'+createLink(controller:'question', action:'list', params:['idTag':tag.id])+'">'
			html += tag.value
			html += '</a></div>'
		}
		html += '</div>'
		out << body() << html
	}
}
